package com.drug.service.impl;

import com.drug.domain.CombineSm;
import com.drug.domain.Result;
import com.drug.domain.SdiStatement;
import com.drug.domain.Statement;
import com.drug.mapper.PodiPdMapper;
import com.drug.mapper.SdiStatementMapper;
import com.drug.mapper.StatementMapper;
import com.drug.service.SettlementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SettlementServiceImpl implements SettlementService {
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private SdiStatementMapper sdiStatementMapper;
    @Autowired
    private PodiPdMapper podiPdMapper;

    /**
     * 查询全部的结算单
     * @param combineSm
     * @return
     */
    @Override
    public Result findAllSm(CombineSm combineSm) {
        Result result = new Result();
        Integer pageNum=1;
        pageNum  = combineSm.getPageNum();
        if (pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Statement> statements=statementMapper.selectAll(combineSm);
        PageInfo pageInfo = new PageInfo(statements);
        result.setData(pageInfo);
        return result;
    }

    @Override
    public Result updateState(Integer[] ids) {
        Result result =new Result();
        if (ids!=null){
            int a=0;
            for (int i = 0; i < ids.length; i++) {
                a=statementMapper.updateState(ids[i]);
                sdiStatementMapper.updateState(ids[i]);
                if (a>1){
                    a=1;
                }
                a++;
            }
            int b =ids.length-a;
            result.setMsg("您的操作成功!成功"+a+"条,失败"+b+"条!");
        }else {
            result.setMsg("请选择!!!");
        }
        return result;
    }

    @Override
    public List<Statement> findAllExport() {
        return statementMapper.selectAllExport();
    }

    @Override
    public Result statementAddDi(String[] id, Integer ssId) {
        Result result = new Result();
        int j=0;
        List<Integer> diidList = new ArrayList<>();
        List<Integer> podiId = new ArrayList<>();
        for (int i = 0; i < id.length; i++) {
            String substring = id[i].substring(0, id[i].indexOf("-"));
            diidList.add(Integer.parseInt(substring));
            String bb=id[i].substring(substring.length()+1, id[i].length());
            podiId.add(Integer.parseInt(bb));
        }
        for (int i = 0; i < diidList.size(); i++) {
            j=   sdiStatementMapper.statementAddDi(diidList.get(i),ssId);
            j++;
        }
        for (int i = 0; i < podiId.size(); i++) {
            //向结算单里添加,修改状态
            podiPdMapper.updatePodiState(podiId.get(i));
        }
        if (j>0){
            result.setMsg("添加成功");
        }
        return result;
    }
}
